package game.module.battleReport.dao;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import db.proto.Protoname.Battlename;
import game.db.DataSourceManager;
import game.module.battleReport.bean.PlayerReport;
import lion.common.MyBlob;

public class PlayReportDBDao {
	private static Logger logger = LoggerFactory.getLogger(PlayReportDBDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static PlayReportDBDao instance = new PlayReportDBDao();
	}

	public static PlayReportDBDao getInstance() {
		return SingletonHolder.instance;
	}

	public boolean addPlayReport(PlayerReport playerreport) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		Battlename playsmsg = playerreport.getPlayers();
		byte content1[] = new byte[0];
		if (playsmsg != null) {
			content1 = playsmsg.toByteArray();
		} 
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			MyBlob blob = new MyBlob(content1);
			int ret = runner.update(conn,
					"insert into battle_report(playerId,game_type,date,battlereportId,roomid,playersmsg)" + "values(?,?,?,?,?,?)",
					playerreport.getPlayerId(), playerreport.getGame_type(), playerreport.getDate(),
					playerreport.getBattleperortId(), playerreport.getRoomid(),blob);
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return addRet;

	}
	
	private ResultSetHandler<PlayerReport> aplayreport = new ResultSetHandler<PlayerReport>() {

		@Override
		public PlayerReport handle(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			PlayerReport playerreport = null;
			if(rs.next()) {
				playerreport = new PlayerReport();
				playerreport.setId(rs.getInt("id"));
				playerreport.setPlayerId(rs.getInt("playerId"));
				playerreport.setGame_type(rs.getByte("game_type"));
				playerreport.setDate(rs.getDate("date"));
				playerreport.setBattleperortId(rs.getLong("battlereportId"));
				playerreport.setRoomid(rs.getString("roomid"));
				try {
					InputStream is = rs.getBinaryStream("playersmsg");
					if(is!=null) {
						Battlename plays = Battlename.parseFrom(is);
						playerreport.setPlayers(plays);
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			return null;
		}
		
	};
	
	private ResultSetHandler<List<PlayerReport>> multihandler = new ResultSetHandler<List<PlayerReport>>() {

		@Override
		public List<PlayerReport> handle(ResultSet rs) throws SQLException {
			List<PlayerReport> playerreports = new ArrayList<PlayerReport>();
			while(rs.next()) {
				PlayerReport playerreport = new PlayerReport();
				playerreport.setId(rs.getInt("id"));
				playerreport.setPlayerId(rs.getInt("playerId"));
				playerreport.setGame_type(rs.getByte("game_type"));
				playerreport.setDate(rs.getDate("date"));
				playerreport.setBattleperortId(rs.getLong("battlereportId"));
				playerreport.setRoomid(rs.getString("roomid"));
				try {
					InputStream is = rs.getBinaryStream("playersmsg");
					if(is!=null) {
						Battlename plays = Battlename.parseFrom(is);
						playerreport.setPlayers(plays);
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
				playerreports.add(playerreport);
			}
			return playerreports;
		}
		
	};
	
	public List<PlayerReport> getPlayerReports(int playerId){
		QueryRunner runner = new QueryRunner(dataSource);
		List<PlayerReport> ret = null;
		try {
			ret = runner.query("select * from battle_report where playerId=? order by id desc limit 10", multihandler,playerId);
		}catch(SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
		
	}
}
